package com.rt.system.service.impl;

import java.util.List;
import com.rt.common.utils.DateUtils;
import com.rt.system.domain.RtAssemblyConf;
import com.rt.system.domain.RtAssemblyDifficulty;
import com.rt.system.domain.RtAssemblyKnowledge;
import com.rt.system.mapper.RtAssemblyConfMapper;
import com.rt.system.mapper.RtAssemblyDifficultyMapper;
import com.rt.system.mapper.RtAssemblyKnowledgeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rt.system.mapper.RtAssemblyInfoMapper;
import com.rt.system.domain.RtAssemblyInfo;
import com.rt.system.service.IRtAssemblyInfoService;

/**
 * 组卷策略信息Service业务层处理
 * 
 * @author Yg
 * @date 2022-05-05
 */
@Service
public class RtAssemblyInfoServiceImpl implements IRtAssemblyInfoService 
{
    @Autowired
    private RtAssemblyInfoMapper rtAssemblyInfoMapper;

    @Autowired
    private RtAssemblyConfMapper rtAssemblyConfMapper;

    @Autowired
    private RtAssemblyDifficultyMapper rtAssemblyDifficultyMapper;

    @Autowired
    private RtAssemblyKnowledgeMapper rtAssemblyKnowledgeMapper;

    /**
     * 查询组卷策略信息
     * 
     * @param id 组卷策略信息主键
     * @return 组卷策略信息
     */
    @Override
    public RtAssemblyInfo selectRtAssemblyInfoById(Long id)
    {
        return rtAssemblyInfoMapper.selectRtAssemblyInfoById(id);
    }

    /**
     * 查询组卷策略信息列表
     * 
     * @param rtAssemblyInfo 组卷策略信息
     * @return 组卷策略信息
     */
    @Override
    public List<RtAssemblyInfo> selectRtAssemblyInfoList(RtAssemblyInfo rtAssemblyInfo)
    {
        return rtAssemblyInfoMapper.selectRtAssemblyInfoList(rtAssemblyInfo);
    }

    /**
     * 新增组卷策略信息
     * 
     * @param rtAssemblyInfo 组卷策略信息
     * @return 结果
     */
    @Override
    public int insertRtAssemblyInfo(RtAssemblyInfo rtAssemblyInfo)
    {
        rtAssemblyInfo.setCreateTime(DateUtils.getNowDate());
        rtAssemblyInfoMapper.insertRtAssemblyInfo(rtAssemblyInfo);
        return rtAssemblyInfo(rtAssemblyInfo);
    }

    /**
     * 修改组卷策略信息
     * 
     * @param rtAssemblyInfo 组卷策略信息
     * @return 结果
     */
    @Override
    public int updateRtAssemblyInfo(RtAssemblyInfo rtAssemblyInfo)
    {
        rtAssemblyInfoMapper.updateRtAssemblyInfo(rtAssemblyInfo);
        return rtAssemblyInfo(rtAssemblyInfo);
    }

    /**
     * 批量删除组卷策略信息
     * 
     * @param ids 需要删除的组卷策略信息主键
     * @return 结果
     */
    @Override
    public int deleteRtAssemblyInfoByIds(Long[] ids)
    {
        return rtAssemblyInfoMapper.deleteRtAssemblyInfoByIds(ids);
    }

    /**
     * 删除组卷策略信息信息
     * 
     * @param id 组卷策略信息主键
     * @return 结果
     */
    @Override
    public int deleteRtAssemblyInfoById(Long id)
    {
        deleteRtAssemvly(id);
        return rtAssemblyInfoMapper.deleteRtAssemblyInfoById(id);
    }

    @Override
    public Integer selectRtAssemblyConfigCount(Long questionTypeId, Long id) {
        return rtAssemblyInfoMapper.selectRtAssemblyConfigCount(questionTypeId,id);
    }

    @Override
    public int updateRtAssemblyStatus(RtAssemblyInfo rtAssemblyInfo) {
        return rtAssemblyInfoMapper.updateRtAssemblyInfo(rtAssemblyInfo);
    }

    // 组卷策略关联表处理
    public int rtAssemblyInfo(RtAssemblyInfo rtAssemblyInfo)
    {
        // 先删除原有的
        deleteRtAssemvly(rtAssemblyInfo.getId());
        for (RtAssemblyConf rtAssemblyConf : rtAssemblyInfo.getRtAssemblyConfs()) {
            rtAssemblyConf.setAssemblyId(rtAssemblyInfo.getId());
            rtAssemblyConfMapper.insertRtAssemblyConf(rtAssemblyConf);
        }
        for (RtAssemblyDifficulty rtAssemblyDifficulty : rtAssemblyInfo.getRtAssemblyDifficulties()) {
            rtAssemblyDifficulty.setAssemblyId(rtAssemblyInfo.getId());
            rtAssemblyDifficultyMapper.insertRtAssemblyDifficulty(rtAssemblyDifficulty);
        }
        for (RtAssemblyKnowledge rtAssemblyKnowledge : rtAssemblyInfo.getRtAssemblyKnowledges()) {
            rtAssemblyKnowledge.setAssemblyId(rtAssemblyInfo.getId());
            rtAssemblyKnowledgeMapper.insertRtAssemblyKnowledge(rtAssemblyKnowledge);
        }
        return 1;
    }

    // 删除关联表
    public void deleteRtAssemvly(Long id){
        rtAssemblyConfMapper.deleteRtAssemblyConfByAId(id);
        rtAssemblyDifficultyMapper.deleteRtAssemblyDifficultyByAId(id);
        rtAssemblyKnowledgeMapper.deleteRtAssemblyKnowledgeByAId(id);
    }
}
